<template>
  <div>
    <div class="gva-form-box">
      <el-form
        :model="formData"
        ref="elFormRef"
        label-position="right"
        :rules="rule"
        label-width="80px"
      >
        <el-form-item label="审核状态:" prop="reviewStatus">
          <el-select
            v-model="formData.reviewStatus"
            placeholder="请选择"
            style="width: 100%"
            :clearable="false"
          >
            <el-option v-for="item in ['']" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺状态:" prop="shopStatus">
          <el-select
            v-model="formData.shopStatus"
            placeholder="请选择"
            style="width: 100%"
            :clearable="false"
          >
            <el-option v-for="item in ['']" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺logo:" prop="shopLogo">
          <SelectImage v-model="formData.shopLogo" file-type="image" />
        </el-form-item>
        <el-form-item label="店铺账号:" prop="shopAccount">
          <el-input
            v-model="formData.shopAccount"
            :clearable="false"
            placeholder="请输入店铺账号"
          />
        </el-form-item>
        <el-form-item label="商家名称:" prop="merchantName">
          <el-input
            v-model="formData.merchantName"
            :clearable="false"
            placeholder="请输入商家名称"
          />
        </el-form-item>
        <el-form-item label="店铺名称:" prop="shopName">
          <el-input v-model="formData.shopName" :clearable="false" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="联系方式:" prop="contact">
          <el-input v-model="formData.contact" :clearable="false" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="店铺简介:" prop="shopIntroduction">
          <RichEdit v-model="formData.shopIntroduction" />
        </el-form-item>
        <el-form-item label="统一社会信用代码:" prop="creditCode">
          <el-input
            v-model="formData.creditCode"
            :clearable="false"
            placeholder="请输入统一社会信用代码"
          />
        </el-form-item>
        <el-form-item label="企业名称:" prop="enterpriseName">
          <el-input
            v-model="formData.enterpriseName"
            :clearable="false"
            placeholder="请输入企业名称"
          />
        </el-form-item>
        <el-form-item label="法定代表人:" prop="legalPerson">
          <el-input
            v-model="formData.legalPerson"
            :clearable="false"
            placeholder="请输入法定代表人"
          />
        </el-form-item>
        <el-form-item label="注册资本:" prop="registeredCapital">
          <el-input
            v-model="formData.registeredCapital"
            :clearable="false"
            placeholder="请输入注册资本"
          />
        </el-form-item>
        <el-form-item label="住所:" prop="registeredAddress">
          <el-input
            v-model="formData.registeredAddress"
            :clearable="false"
            placeholder="请输入住所"
          />
        </el-form-item>
        <el-form-item label="成立日期:" prop="establishmentDate">
          <el-date-picker
            v-model="formData.establishmentDate"
            type="date"
            placeholder="选择日期"
            :clearable="false"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="营业期限:" prop="businessTerm">
          <el-input
            v-model="formData.businessTerm"
            :clearable="false"
            placeholder="请输入营业期限"
          />
        </el-form-item>
        <el-form-item label="经营范围:" prop="businessScope">
          <el-input
            v-model="formData.businessScope"
            :clearable="false"
            placeholder="请输入经营范围"
          />
        </el-form-item>
        <el-form-item label="营业执照电子版:" prop="businessLicense">
          <SelectFile v-model="formData.businessLicense" />
        </el-form-item>
        <el-form-item label="法人身份证:" prop="legalPersonIdCard">
          <SelectFile v-model="formData.legalPersonIdCard" />
        </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
  import { createStore, updateStore, findStore } from '@/api/Shop/store'

  defineOptions({
    name: 'StoreForm',
  })

  // 自动获取字典
  import { getDictFunc } from '@/utils/format'
  import { useRoute, useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { ref, reactive } from 'vue'
  // 图片选择组件
  import SelectImage from '@/components/selectImage/selectImage.vue'
  // 文件选择组件
  import SelectFile from '@/components/selectFile/selectFile.vue'
  // 富文本组件
  import RichEdit from '@/components/richtext/rich-edit.vue'

  const route = useRoute()
  const router = useRouter()

  // 提交按钮loading
  const btnLoading = ref(false)

  const type = ref('')
  const formData = ref({
    shopLogo: '',
    shopAccount: '',
    merchantName: '',
    shopName: '',
    contact: '',
    shopIntroduction: '',
    creditCode: '',
    enterpriseName: '',
    legalPerson: '',
    registeredCapital: '',
    registeredAddress: '',
    establishmentDate: new Date(),
    businessTerm: '',
    businessScope: '',
    businessLicense: [],
    legalPersonIdCard: [],
  })
  // 验证规则
  const rule = reactive({
    reviewStatus: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
    shopStatus: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
    creditCode: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
    enterpriseName: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
    legalPerson: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
    businessScope: [
      {
        required: true,
        message: '',
        trigger: ['input', 'blur'],
      },
    ],
  })

  const elFormRef = ref()

  // 初始化方法
  const init = async () => {
    // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findStore({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
  }

  init()
  // 保存按钮
  const save = async () => {
    btnLoading.value = true
    elFormRef.value?.validate(async (valid) => {
      if (!valid) return (btnLoading.value = false)
      let res
      switch (type.value) {
        case 'create':
          res = await createStore(formData.value)
          break
        case 'update':
          res = await updateStore(formData.value)
          break
        default:
          res = await createStore(formData.value)
          break
      }
      btnLoading.value = false
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '创建/更改成功',
        })
      }
    })
  }

  // 返回按钮
  const back = () => {
    router.go(-1)
  }
</script>

<style></style>
